package com.zerdoor.controller;

import com.zerdoor.model.ChatingRecord;
import com.zerdoor.model.UserInfo;
import com.zerdoor.pagehelper.PageHelper;
import com.zerdoor.pagehelper.PageInfo;
import com.zerdoor.service.IChatManageService;
import com.zerdoor.util.chatRecord.ChatingRecordOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zhongjieyu on 2014/11/11.
 * 任务房间控制层
 */
@Controller
@RequestMapping("/chatManage")
public class ChatManageController {

    @Autowired
    private IChatManageService chatManageService;

    /**
     * 跳转到任务房间页面
     * @return
     */
    @RequestMapping(value = "/chatHouse",method = RequestMethod.GET)
    public String chatHouse(String contract_id,HttpServletRequest request){

        request.setAttribute("channel",contract_id);

        return "/chat/chatHouse";
    }

    /**
     * 跳转到房间首页,查询该用户正在进行的项目
     * @param userType 用户类型 0为艺术家 1为发包方
     * @param itemId 任务ID
     * @param request
     * @return
     */
    @RequestMapping(value = "/chatMain", method = RequestMethod.GET)
    public String findInItemNameByUserId(String userType,String itemId,HttpServletRequest request) {

        try {

            UserInfo userInfo = (UserInfo) request.getSession().getAttribute("curUser");

            int userId = userInfo.getUserId();

            //得到正在进行中的项目
            List<Map<String,String>> list = chatManageService.findInItemNameByUserId(userType,userId);

            request.setAttribute("inItemList",list);
            request.setAttribute("userType",userType);
            request.setAttribute("itemId",itemId);


            //ResponseEntity<List<Map<String,String>>> responseEntity = new ResponseEntity<List<Map<String,String>>>(list, HttpStatus.OK);


        } catch (Exception e) {

            e.printStackTrace();

           // ResponseEntity<String> responseEntity = new ResponseEntity<String>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);

        }

        return "/chat/chatMain";
    }

    /**
     * 根据项目ID查询项目合同
     * @return
     */
    @RequestMapping(value = "/findContractByItemId", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<?> findContractByItemId(String item_id,String type,HttpServletRequest request) {


        try {

            UserInfo userInfo = (UserInfo) request.getSession().getAttribute("curUser");

            Map param = new HashMap();
            param.put("item_id",item_id);

            if("1".equals(type)){

                param.put("baile_id",userInfo.getBaileId());

            }else{
                param.put("artist_id",userInfo.getArtistId());
            }

            //得到正在进行中的项目
            List<Map<String,String>> list = chatManageService.findContractByItemId(param);

            ResponseEntity<List<Map<String,String>>> responseEntity = new ResponseEntity<List<Map<String,String>>>(list, HttpStatus.OK);

            return responseEntity;
        } catch (Exception e) {

            e.printStackTrace();

            ResponseEntity<String> responseEntity = new ResponseEntity<String>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
            return responseEntity;
        }




    }



    /**
     * 查询任务房间聊天记录
     * @return
     */
    @RequestMapping(value = "/findChatingRecordList", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<?> findChatingRecordList(String channel,HttpServletRequest request) {


        try {
            String path = request.getSession().getServletContext().getRealPath("") ;


            //得到聊天记录实体集合
            List<ChatingRecord> list = chatManageService.findChatingRecordList(channel,path);


            ResponseEntity<List<ChatingRecord>> responseEntity = new ResponseEntity<List<ChatingRecord>>(list, HttpStatus.OK);

            return responseEntity;
        } catch (Exception e) {

            e.printStackTrace();

            ResponseEntity<String> responseEntity = new ResponseEntity<String>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
            return responseEntity;
        }




    }
}
